package com.zzx.controller;

import com.zzx.Util.DateFormatUtil;
import com.zzx.Util.JSONUtil;
import com.zzx.Util.UserUtil;
import com.zzx.bean.BootstrapTableExpress;
import com.zzx.bean.Express;
import com.zzx.bean.Message;
import com.zzx.bean.User;
import com.zzx.service.imp.ExpressService;
import com.zzx.servlet.ResponseText;
import com.zzx.servlet.ResponseView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Author: Spider Man
 * @Description:
 * @Date Created in 2020-09-17 16:09
 * @Modified By:
 */
public class QRCodeController {

    ExpressService expressService = new ExpressService();

    @ResponseView("/wx/createQRCode.do")
    public String createQRCode(HttpServletRequest request, HttpServletResponse response){
        String code = request.getParameter("code");
        String type = request.getParameter("type");
        String uPhone = null;
        String QRCodeContent = null;
        if ("express".equals(type)){
            //快递二维码
            QRCodeContent = "express_"+code;
        }else {
            //用户二维码
            try {
                User wxUser = UserUtil.getWxUser(request.getSession());
                uPhone = wxUser.getuPhone();
                QRCodeContent = "user_"+uPhone;
            }catch (NullPointerException e){
                return "/500.html";
            }
        }
        HttpSession session = request.getSession();
        session.setAttribute("qrcode",QRCodeContent);
        return "/personQRcode.html";
    }

    @ResponseText("/wx/qrcode.do")
    public String getQRCode(HttpServletRequest request, HttpServletResponse response){
        HttpSession session = request.getSession();
        String qrcode = (String) session.getAttribute("qrcode");
        Message message = new Message();
        if (qrcode == null){
            message.setStatus(-1);
            message.setResult("取件码获取出错，请用户重新操作");
        }else {
            message.setStatus(0);
            message.setResult(qrcode);
        }
        return JSONUtil.toJSON(message);
    }

    @ResponseText("/wx/updateStatus.do")
    public  String updateExpressStatus(HttpServletRequest request, HttpServletResponse response){
        String code = request.getParameter("code");
        boolean flag = expressService.updateStatus(code);
        Message message = new Message();
        if (flag){
            message.setStatus(0);
            message.setResult("取件成功");
        }else {
            message.setStatus(-1);
            message.setResult("取件失败,请尝试更新二维码");
        }
        return JSONUtil.toJSON(message);
    }

    @ResponseText("/wx/findExpressByCode.do")
    public String findExpressByCode(HttpServletRequest request, HttpServletResponse response){
        String code = request.getParameter("code");
        Express express = expressService.findExpressByCode(code);
        BootstrapTableExpress bootstrapTableExpress = null;
        if (express != null){
            bootstrapTableExpress = new BootstrapTableExpress(express.getId(),express.getNumber(),
                    express.getUsername(),express.getUserPhone(),express.getCompany(),express.getCode(), DateFormatUtil.format(express.getInTime()),
                    express.getOutTime()==null?"未出库":DateFormatUtil.format(express.getOutTime()),express.getStatus()==0?"待取件":"已取件",express.getSysPhone());
        }
        Message message = new Message();
        if (express == null){
            message.setStatus(-1);
            message.setResult("取件码不存在");
        }else {
            message.setStatus(0);
            message.setResult("查询成功");
            message.setData(bootstrapTableExpress);
        }
        return JSONUtil.toJSON(message);
    }
}
